﻿
Imports osi.root.envs
Imports osi.root.constants
Imports osi.root.connector
Imports osi.root.utils.error_handle
Imports lock_t = osi.root.lock.lock(Of osi.root.lock.slimlock.lazylock)

Namespace error_handle
    Public Module _error_handle
        Public Const error_type_count As Int64 = error_type.last - error_type.first + 1
        Public Const error_type_char As String = "_aceiswupo_"
        Public ReadOnly error_type_defination() As String = {"_",
                                                             "application",
                                                             "critical",
                                                             "exclamation",
                                                             "information",
                                                             "system",
                                                             "warning",
                                                             "user",
                                                             "performance",
                                                             "other",
                                                             "_"}
        Public Const seperator As String = character.comma + character.blank

        Sub New()
            If strlen(error_type_char) <> error_type_count Then
                k_raise_error("error_type_char length <> error_type count")
            End If
            If error_type_defination.Length() <> error_type_count Then
                k_raise_error("error_type_defination length <> error_type count")
            End If
        End Sub
    End Module
End Namespace

Public Module _error_handle
    Sub New()
        If debug_trace Then
            set_debug_mode()
        End If
        If has_priority_defined Then
            current_process.PriorityClass() = priority_class
        End If
        _error_counter.trigger()
        raise_error(error_type.application,
                    "start error_handle for process ",
                    application_name,
                    ", ver ",
                    application_version,
                    ", process id ",
                    current_process.Id(),
                    ", tf changeset id ",
                    tf_latest_changeset_id,
                    ", built at ",
                    buildtime)
        raise_error("detail build info as",
                    ", time ",
                    buildtime,
                    ", machine ",
                    buildmachine,
                    ", user ",
                    builduser,
                    ", system ver ",
                    buildsysver,
                    ", processor ",
                    buildprocessor)
        raise_error("detail tfs info as",
                    ", changeset id ",
                    tf_latest_changeset_id,
                    ", submitted by ",
                    tf_latest_changeset_user,
                    ", at ",
                    tf_latest_changeset_date,
                    ", comment ",
                    tf_latest_changeset_comment)
    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Sub raise_error(ByVal errmsg As String,
                           Optional ByVal errtype As error_type = error_type.information,
                           Optional ByVal errtypeChar As Char = character.null,
                           Optional ByVal additional_jump As Int32 = 0)
        error_handle_event.raise_event(errtype, errtypeChar, errmsg)
        error_distributor.distribute(errtype, errtypeChar, errmsg, additional_jump + 1)
    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Sub raise_error(ByVal errtype As error_type,
                           ByVal errtypeChar As Char,
                           ByVal additionaljump As Int32,
                           ByVal ParamArray os() As Object)
        raise_error(patchup_error_msg(os), errtype, errtypeChar, additionaljump + 1)
    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Sub raise_error(ByVal errType As error_type,
                           ByVal errTypeChar As Char,
                           ByVal ParamArray os() As Object)
        raise_error(errType, errTypeChar, 1, os)
    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Sub raise_error(ByVal errType As error_type,
                           ByVal ParamArray os() As Object)
        raise_error(errType, character.null, 1, os)
    End Sub

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Sub raise_error(ByVal ParamArray os() As Object)
        raise_error(error_type.information, character.null, 1, os)
    End Sub
End Module
